package top.tocchen.bookshop.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import top.tocchen.bookshop.enums.HttpStatusEnum;

import java.io.Serializable;

/**
 * 新增类,提供{@link HttpStatusEnum}内容的解析
 *
 * 1.添加支持HttpStatusEnum的枚举
 * 2.添加HttpStatusEnum下返回success或fail的方法
 *
 * @author tocchen
 * @date 2022/6/19 18:33
 * @since jdk 1.8
 **/
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 2019090611411L;

    private int status;

    private String msg;

    private T data;

    private String token;

    public Response() {
    }

    private Response(HttpStatusEnum statusEnum){
        this(statusEnum,null);
    }

    private Response(HttpStatusEnum statusEnum, T data){
        this(statusEnum,data,null);
    }

    private Response(HttpStatusEnum statusEnum, T data, String token) {
        this.status = statusEnum.getStatus();
        this.msg = statusEnum.getMessage();
        this.data = data;
        this.token = token;
    }

    /**
     * 请求成功
     * @return Result对象
     */
    public static Response success(){
        return new Response(HttpStatusEnum.SUCCESS_STATUS);
    }

    /**
     * 请求成功
     * @param statusEnum HttpStatusEnum实体类
     * @return Result对象
     */
    public static Response success(HttpStatusEnum statusEnum){
        return new Response(statusEnum);
    }

    /**
     * 请求成功 (泛型方法)
     * @param statusEnum HttpStatusEnum实体类
     * @param data 需要传出的对象
     * @param <E> 需要传出对象的数据类型
     * @return Result对象
     */
    public static <E> Response<E> success(HttpStatusEnum statusEnum, E data){
        return new Response<E>(statusEnum,data);
    }

    /**
     *请求成功,并携带token值放回
     * @param statusEnum HttpStatusEnum实体类
     * @param token token信息
     * @return Result对象
     */
    public static <E> Response<E> success(HttpStatusEnum statusEnum, E date , String token){
        return new Response<E>(statusEnum,date,token);
    }


    public static Response fail(HttpStatusEnum statusEnum){
        return new Response(statusEnum);
    }

    public static <E> Response<E> fail(HttpStatusEnum statusEnum, E data){
        return new Response<E>(statusEnum,data);
    }

}
